#!/usr/bin/python
# -*- coding: utf-8 -*-
import configparser
from enum import Enum
import os
import traceback


class ValueTypeEnum(Enum):
    STRING = 'str'
    NONE = 'str'
    INT = 'int'
    BOOL = 'bool'


class Config:
    def __init__(self):
        version = "dev"
        base_path = os.path.dirname(os.path.abspath(__file__))
        try:
            self.con_base = configparser.ConfigParser()
            self.con_base.read(base_path + os.sep + "config.ini", encoding='utf-8')
            version = self.con_base.get('base', 'version')
        except:
            pass
        yaml_path = "config.{}.ini"
        yaml_path = yaml_path.format(version) if os.path.exists(yaml_path.format(version)) else yaml_path.format('dev')
        # 创建配置文件对象
        self.con = configparser.ConfigParser()
        # 读取文件
        self.con.read(base_path + os.sep + yaml_path, encoding='utf-8')

    def get(self, section: str, key: str, value_type=ValueTypeEnum.STRING):
        try:
            value_type = value_type if isinstance(value_type, ValueTypeEnum) else ValueTypeEnum.STRING
            if section and section in self.con.sections() and key:
                result = {
                    'bool': self.con.getboolean,
                    'int': self.con.getint,
                    'str': self.con.get,
                    'None': self.con.get
                }[value_type.value](section, key, raw=True)
                return result if result else None
        except Exception as e:
            traceback.print_exc()
        return None


config = Config()
# print(config.get("graph_match", "audit_platform_base1"))
